/**
* 提交回复
*/
function  post() {
    var questionId = $("#question_id").val();
    var content = $("#commment_content").val();
    comment2target(questionId,1,content);
}

/**
 * 抽取方法
 */

function comment2target(targetId,type,content) {
    if (!content){
        alert("不能回复空内容");
        return;
    }
    $.ajax({
        type: "POST",
        url: "/comment",
        contentType: 'application/json',
        data: JSON.stringify({
            "parentId":targetId,
            "content":content,
            "type":type
        }),
        success: function (response) {
            if (response.code == 200){
                window.location.reload();
            }else {
                if (response.code == 2003){
                    var isAccepted = confirm(response.message)
                    if(isAccepted){
                        window.open("https://gitee.com/oauth/authorize?client_id=c7270d1a2792eb66cf8f1bd83d2ec454dd7f67d8b27d818c4841a01504109e3e&redirect_uri=http://localhost/callback&response_type=code&state=1")
                        window.localStorage.setItem("closable",true);
                    }
                }else {
                    alert(response.message)
                }
            }
        },
        dataType: "json"
    });

}

/**
 * 提交二级评论
*/
function comment(e) {
    var commentId = e.getAttribute("data-id");
    var comment = $("#input-"+ commentId ).val();
    comment2target(commentId,2,comment);
}


/**
 *展开二级回复
 */

function callapseComments(e) {
    var id = e.getAttribute("data-id");
    var comments = $("#comment-"+ id);
    /*获取二级状态的展开转台*/
   var collapse =  e.getAttribute("data-collapse");

   if (collapse){
       /*折叠二级评论*/
       comments.removeClass("in");
       e.removeAttribute("data-collapse");
       /*删除active*/
       e.classList.remove("active")
   }else {

       $.getJSON( "/comment/"+id, function( data ) {

           var subCommentContainer = $("#comment-"+ id);

           if (subCommentContainer.children().length !=1){
               /*展开二级评论*/
               comments.addClass("in")
               /*标记展开状态*/
               e.setAttribute("data-collapse","in");
               /*添加active*/
               e.classList.add("active");
           }else {


               $.each( data.data.reverse(), function( index, comment ) {

                   var mediaLeftElement = $("<div/>",{
                       "class":"media-left"
                   }).append($("<img/>",{
                       "class":"media-object img-rounded",
                       "src":comment.user.avatarUrl
                   }));

                   var mediaBodyElement = $("<div/>",{
                       "class":"media-body"
                   }).append($("<h5/>",{
                       "class":"media-heading",
                       "html":comment.user.name
                   })).append($("<div/>",{
                       "html":comment.content
                   })).append($("<div/>",{
                       "class":"menu"
                   }) ).append($("<span/>",{
                       "class":"pull-right",
                       "html":moment(comment.gmtCreate).format("YYYY-MM-DD")
                   }))


                   var mediaElement = $("<div/>",{
                       "class":"media"
                   }).append(mediaLeftElement).append(mediaBodyElement)


                var commentElement = $("<div/>",{
                    "class":"col-lg-12 col-md-12 col-xs-12 col-sm-12",
                }).append(mediaElement);

                   subCommentContainer.prepend(commentElement)

               });


               /*展开二级评论*/
               comments.addClass("in")
               /*标记展开状态*/
               e.setAttribute("data-collapse","in");
               /*添加active*/
               e.classList.add("active");
           }


       });

   }

}


function showSelectTag() {
    $("#select-tag").show();
}

function selectTag(e) {

    let value = e.getAttribute("data-tag");


    var previous = $("#tig").val();

    if (previous.indexOf(value) != -1){

    }else {
        if (previous){
            $("#tig").val(previous + ',' + value);
        }else {
            $("#tig").val(value);
        }
    }

}


